/*
 * WaveInterstitial - is the game won? should the server know?
 *
 * Copyright 2007 James W. Hofmann
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#if flash9
import flash.Lib;
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.display.Graphics;
import flash3f.BitFont;
import flash3f.InputHandler;
#else neko
import neko3f.InputHandler;
#end

class WaveInterstitial
// shows a status update...score etc.
// does gameovers, as well
{

	#if flash9
	static public var header : Bitmap = new Bitmap();
	static public var header2 : Bitmap = new Bitmap();
	static public var score : Bitmap = new Bitmap();
	static public var territory : Bitmap = new Bitmap();
	static public var kills : Bitmap = new Bitmap();
	static public var killbonus : Bitmap = new Bitmap();
	static public var clicktocontinue : Bitmap = new Bitmap();
	static public var sendscore : Shiner;
	static public var visitsite : Shiner;
	static public var matting : Sprite;
	#end

	static public var victory : Bool;
	static public var defeat : Bool;
	
	static public function start()
	{
	
		victory = false;
		defeat = false;

		var overcount = 0;
		
		for (n in MapGen.castles)
		{
			if (n.fort==null || !n.fort.iscastle || !n.fort.castlecontrol)
			{
				overcount++;
			}
		}

		if (overcount>=3)
		{
			defeat=true;
		}
		else if (Game.cur.fight.wave==10)
		{
			victory=true;
		}
	
		var terrpoints : Int = FortressManager.calcterritory();
		var killpoints : Int = Game.cur.fight.killsthisround;
		var bonus : Bool;
		
		if (Game.cur.creepman.allcreeps.length>0)
			bonus = false;
		else
			bonus = true;

		var ppter : Int;
		var ppkill : Int;
		var ppclean : Int;

		switch(Game.cur.gamemode)
		{
			case(0): // normal
				ppter = 25;
				ppkill = 10;
				ppclean = 200;
			case(1): // hard
				ppter = 50;
				ppkill = 30;
				ppclean = 500;
		}
		
		Game.cur.score+=((terrpoints*ppter)+(killpoints*ppkill));
			
		if (bonus)
			Game.cur.score+=Game.cur.fight.wave*ppclean;

		#if flash9
				
		header.bitmapData = BitFont.gen(["GAME #",Title.seed]);
		territory.bitmapData = BitFont.gen(["TERRITORY: ",terrpoints," X ",ppter]);
		kills.bitmapData = BitFont.gen(["KILLS: ",killpoints," X ",ppkill]);
		killbonus.bitmapData = BitFont.gen(["CLEAN MAP BONUS: ",Game.cur.fight.wave*200]);
		score.bitmapData = BitFont.gen(["SCORE: ",Game.cur.score]);

		clicktocontinue.bitmapData = BitFont.gen(["CLICK TO CONTINUE"]);
		clicktocontinue.visible = false;	
		sendscore = new Shiner(Fonts.verdanaitalic.gen(["*Send Score*"]));
		sendscore.visible = false;

		visitsite = new Shiner(Fonts.verdanaitalic.gen(["Check your scores at spacecreeps.com"]));
		visitsite.visible = false;
		
		if (defeat)
		{
			SFX.gameover=true;
			header2.bitmapData = BitFont.gen(["FAILURE AT WAVE ",Game.cur.fight.wave]);
			if (Recorder.recordstate!=3)
			{
				sendscore.visible = true;
				visitsite.visible = true;
			}
		}
		else if (victory)
		{
			SFX.gameover=true;
			header2.bitmapData = BitFont.gen(["VICTORY!"]);
			if (Recorder.recordstate!=3)
			{
				sendscore.visible = true;
				visitsite.visible = true;
			}
		}
		else
		{
			header2.bitmapData = BitFont.gen(["WAVE ",Game.cur.fight.wave," COMPLETE"]);
			clicktocontinue.visible = true;
		}
		
		if (bonus)
			killbonus.visible=true;
		else
			killbonus.visible=false;
		
		header.x = (680/2)-(header.bitmapData.width/2);
		header2.x = (680/2)-(header2.bitmapData.width/2);
		territory.x = (680/2)-(territory.bitmapData.width/2);
		kills.x = (680/2)-(kills.bitmapData.width/2);
		killbonus.x = (680/2)-(killbonus.bitmapData.width/2);
		score.x = (680/2)-(score.bitmapData.width/2);
		clicktocontinue.x = (680/2)-(clicktocontinue.bitmapData.width/2);
		sendscore.x = (680/2)-(sendscore.bitmapData.width/2);
		visitsite.x = (680/2)-(visitsite.bitmapData.width/2);

		header.y = (425/2)-(425/10*3);
		header2.y = (425/2)-(425/10*2);
		territory.y = (425/2)-(425/10*1);
		kills.y = (425/2);
		killbonus.y = (425/2)+(425/10*1);
		score.y = (425/2)+(425/10*2);
		clicktocontinue.y = (425/2)+(425/10*3);
		sendscore.y = (425/2)+(425/10*3);
		visitsite.y = (425/2)+(425/10*3)+30;

		matting = new Sprite();
		matting.graphics.clear();
		matting.graphics.lineStyle(2,0x000008,0.25);
		matting.graphics.beginFill(0x000008,0.75);
		
		for (n in [header, header2, territory, kills, killbonus, score, clicktocontinue])
		{

			if (n.visible)
				matting.graphics.drawRect(0,n.y-6,680,n.bitmapData.height+12);
		
		}

		
		Lib.current.addChild(matting);
		Lib.current.addChild(header);
		Lib.current.addChild(header2);
		Lib.current.addChild(territory);
		Lib.current.addChild(kills);
		Lib.current.addChild(killbonus);
		Lib.current.addChild(score);
		Lib.current.addChild(clicktocontinue);
		
		#else neko
		
			trace(["wave ",Game.cur.fight.wave]);
			if (defeat)
			{
				trace("defeat");
			}
			else if (victory)
			{
				trace("victory");
			}
		#end

		if (Game.cur.fight.wave<10)
			Game.cur.fight.wave+=1;

	}
	
	static public function end()
	{
		#if flash9

		Lib.current.removeChild(header);
		Lib.current.removeChild(header2);
		Lib.current.removeChild(territory);
		Lib.current.removeChild(kills);
		Lib.current.removeChild(killbonus);
		Lib.current.removeChild(score);
		Lib.current.removeChild(clicktocontinue);
		Lib.current.removeChild(matting);
		
		header.bitmapData.dispose();
		header2.bitmapData.dispose();
		territory.bitmapData.dispose();
		kills.bitmapData.dispose();
		killbonus.bitmapData.dispose();
		score.bitmapData.dispose();
		clicktocontinue.bitmapData.dispose();
		
		#end
		
	}
	
	static public function up()
	{
	
		#if flash9
		if (Networker.successfulup == true || Title.challengeseed!=Title.seed)
		{
			sendscore.visible = false;
		}
		#end
	
		if (!(victory || defeat))
		{
			if (InputHandler.click)
			{			
				ComponentManager.remove(WaveInterstitial);
			}
		}
		#if flash9
		else
		{
			if (InputHandler.click && sendscore.visible && sendscore.bitmapData.rect.contains
			(InputHandler.cursx-sendscore.x,InputHandler.cursy-sendscore.y))
			{
				Networker.go("Recupload");
			}

			if (InputHandler.click && visitsite.visible && visitsite.bitmapData.rect.contains
			(InputHandler.cursx-visitsite.x,InputHandler.cursy-visitsite.y))
			{
					var baseurl = "http://spacecreeps.com/creepsearchresult.php?type=Score&prev=0&query=";
					Lib.getURL(new flash.net.URLRequest(baseurl),"_blank");			
			}
			
		}
		#end
	}

}
